import { Hls } from './models/Hls';

export class utils {
  static toHexString(num: number) {
    return num.toString(16).toUpperCase();
  }

  static toHls(num: number) {
    let b = num & 0xff, g = (num >> 8) & 0xff, r = (num >> 16) & 0xff;
    let rgb = [r, g, b], maxc = Math.max(...rgb), minc = Math.min(...rgb),
      l = (minc + maxc) / 2.0;
    if (minc == maxc)
      return new Hls(0.0, l, 0.0);
    let s, h = 0.0;
    if (l <= 0.5) s = (maxc - minc) / (maxc + minc);
    else s = (maxc - minc) / (2.0 - maxc - minc);
    var rc = (maxc - rgb[0]) / (maxc - minc);
    var gc = (maxc - rgb[1]) / (maxc - minc);
    var bc = (maxc - rgb[2]) / (maxc - minc);
    if (rgb[0] == maxc) h = bc - gc;
    if (rgb[1] == maxc) h = 2.0 + rc - bc;
    if (rgb[2] == maxc) h = 4.0 + gc - rc;
    h = h / 6.0 % 1.0;
    return new Hls(h * 360, l * 100, s * 100);
  }
}